package com.liusu.spider.js;

import com.liusu.spider.wuzhou.Dict;
import org.apache.commons.codec.binary.Base64;
import org.junit.jupiter.api.Test;

import java.util.*;

public class JavaScript {

    public static Object[] VideoListJson;
    public static Map<String,Object> videoUrl;
    static {
        VideoListJson = new Object[]{
                new Object[]{
                        "百度影音",
                        new String[]{
                                "\u7B2C01\u96C6$https://v5.dious.cc/20210406/puZHKZuI/index.m3u8$bdhd","\u7B2C02\u96C6$https://v5.dious.cc/20210406/BBDwORew/index.m3u8$bdhd","\u7B2C03\u96C6$https://v5.dious.cc/20210406/VEdr6ATN/index.m3u8$bdhd","\u7B2C04\u96C6$https://v5.dious.cc/20210406/zufEIyYW/index.m3u8$bdhd","\u7B2C05\u96C6$https://v5.dious.cc/20210406/bbUqQDf5/index.m3u8$bdhd","\u7B2C06\u96C6$https://v5.dious.cc/20210406/8styX2xb/index.m3u8$bdhd","\u7B2C07\u96C6$https://v5.dious.cc/20210406/Y3RvrL7m/index.m3u8$bdhd","\u7B2C08\u96C6$https://v5.dious.cc/20210406/HGLaKYFF/index.m3u8$bdhd","\u7B2C09\u96C6$https://v5.dious.cc/20210406/Fmeoc60D/index.m3u8$bdhd","\u7B2C10\u96C6$https://v5.dious.cc/20210406/RhvNq5qk/index.m3u8$bdhd","\u7B2C11\u96C6$https://v5.dious.cc/20210406/wgFXgLrZ/index.m3u8$bdhd","\u7B2C12\u96C6$https://v5.dious.cc/20210406/XgHzKRgn/index.m3u8$bdhd","\u7B2C13\u96C6$https://v5.dious.cc/20210406/4PV5PfCL/index.m3u8$bdhd"

                        }
                },
                new Object[]{
                        "百度影音",
                        new String[]{
                                "\u7B2C01\u96C6$https://v5.dious.cc/20210406/puZHKZuI/index.m3u8$bdhd","\u7B2C02\u96C6$https://v5.dious.cc/20210406/BBDwORew/index.m3u8$bdhd","\u7B2C03\u96C6$https://v5.dious.cc/20210406/VEdr6ATN/index.m3u8$bdhd","\u7B2C04\u96C6$https://v5.dious.cc/20210406/zufEIyYW/index.m3u8$bdhd","\u7B2C05\u96C6$https://v5.dious.cc/20210406/bbUqQDf5/index.m3u8$bdhd","\u7B2C06\u96C6$https://v5.dious.cc/20210406/8styX2xb/index.m3u8$bdhd","\u7B2C07\u96C6$https://v5.dious.cc/20210406/Y3RvrL7m/index.m3u8$bdhd","\u7B2C08\u96C6$https://v5.dious.cc/20210406/HGLaKYFF/index.m3u8$bdhd","\u7B2C09\u96C6$https://v5.dious.cc/20210406/Fmeoc60D/index.m3u8$bdhd","\u7B2C10\u96C6$https://v5.dious.cc/20210406/RhvNq5qk/index.m3u8$bdhd","\u7B2C11\u96C6$https://v5.dious.cc/20210406/wgFXgLrZ/index.m3u8$bdhd","\u7B2C12\u96C6$https://v5.dious.cc/20210406/XgHzKRgn/index.m3u8$bdhd","\u7B2C13\u96C6$https://v5.dious.cc/20210406/4PV5PfCL/index.m3u8$bdhd"

                        }
                }
        };
        /**
         * 城市信息
         */
        videoUrl = new HashMap<>();
        videoUrl.put("code","A00000");
        Map<String,String>  data = new HashMap<>();
        data.put("l","https://v-ddccb004.71edge.com/v.mp4?uuid=7c819a46-611331b7-212&src=iqiyi.com");
        data.put("t","CNC|ShanDong_QingDao-124.129.154.70");
        data.put("mrc","0");
        data.put("z","vcdn_Rcache_taiyuan8_cnc");
        data.put("time","1628647863");
        data.put("httpsock","1");
        data.put("v","2021-08-02 v21.31.1");
        data.put("direct_h2c","1");
        videoUrl.put("data",data);
    }


    /**
     *
     * @param info
     * @param cookie
     * @return
     */
    public String getDrmToken(Map<String,String> info,Map<String,String> cookie){

        String t = info.get("termId");
        String n = t;
        String u = info.get("cid");
        String c = u;
        Map<String,String> s = initial(info,new String[]{"termId", "cid"});

        String l = cookie.get("uin");
        String d = cookie.get("skey");
        String f = cookie.get("p_skey");
        String p = cookie.get("p_lskey");
        String h = cookie.get("uid_a2");
        String y = cookie.get("uid_type");
        String v = cookie.get("uid_origin_uid_type");
        String m = cookie.get("uid_origin_auth_type");
        String g = cookie.get("uid_appid");
        String[] _ = new String[9];
        _[0] = "uin=" + l;
        _[1] = "skey=" + d;
        _[2] = "pskey=" + f;
        _[3] = "plskey=" + p;
        _[4] = "ext=" + h;
        int index = 5;
        if(g!=""){
            _[index]="uid_appid=" + g;
            index++;
        }
        if(y!=""){
            _[index]="uid_type=" + y;
            index++;
        }

        if(v!=""){
            _[index]="uid_origin_uid_type=" + v;
            index++;
        }
        if(m!=""){
            _[index]="uid_origin_auth_type=" + m;
            index++;
        }
        if(c!=""){
            _[index]="cid=" + c;
            index++;
        }
        if(n!=""){
            _[index]="term_id=" + n;
        }
        Set<String> ks = s.keySet();
        String[] b = new String[ks.size()];
        int index1 = 0;
        for(String k: s.keySet()){
            b[index1] = k+"="+s.get(k);
            index1++;
        }

        String[] x = this.water(new ArrayList<String>(),Arrays.asList(_));

        String[] P = this.water(Array_.asList(x), Array_.asList(b));


        String token = Base64.encodeBase64String(String.join(";",P).getBytes());






        return null;
    }


    public String[] water(List<String> e,List<String> t){
        int r = t.size();
        int o = e.size();


        for(int i = 0;i<t.size();i++){


            e.add(o,t.get(i));


            o++;

        }
        return e.toArray(new String[e.size()]);
    }

    public String[] wind(List<String> e,List<String> t){
        return null;
    }

    public Map<String,String> initial(Map<String,String> info,String[] keys){

        Map<String,String> i = new HashMap<>();
        for(String k: info.keySet()){
            if(Array_.contain(keys,k)){
                continue;
            }
            i.put(k,info.get(k));
        }

        return i;
    }

































    public String get_vid(Integer from,Integer to,String url){
        /**
         * 视频的参数信息
         */
        String[] list = this.get_video_param(from,to);
        return list[1];
    }

    public String get_iframe_src(Integer from,Integer to,String url){
        /**
         * 视频的参数信息
         */
        String[] list = this.get_video_param(from,to);
        String t = ((Map<String,String>)videoUrl.get("data")).get("t");
        int start = t.indexOf("-");
        t = t.substring(0,start);
        int start1 = t.indexOf("|")+1;
        t = t.substring(start1);

        String src = "https://saas.jialingmm.net/code.php?type=" + list[2] + "&vid=" + list[1] + "&userlink=" + this.url_convert(url) + "&adress=" + t;
        return src;
    }


    public String[] get_video_param(Integer from,Integer to){
        int length = VideoListJson.length;
        if(length > 0){
            String[] video_list_param = (String[]) (((Object[])VideoListJson[Math.min(from,length-1)])[1]);

            return video_list_param[Math.min(to,video_list_param.length-1)].split("\\$");
        }
        return new String[]{"","",""};
    }

    public String url_convert(String url){
        return url.replaceAll("/","%2F").replace(":","%3A");
    }


}


